package com.lkk.cg.service.impl;

import com.lkk.cg.base.DaoSupportImpl;
import com.lkk.cg.domain.Role;
import com.lkk.cg.service.RoleService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lkk on 2014/12/3.
 */
@Transactional
//@Service
public class RoleServiceImpl extends DaoSupportImpl<Role> implements RoleService {

    public Role getDefaultRole() {
        Session session = getSession();
        Query query = session.createQuery("from Role r where r.isDefault=?")
                .setParameter(0, true);
        return (Role) query.uniqueResult();
    }

    public void setDefaultRole(Long id) {
        Session session = getSession();
        Role originDefault = getDefaultRole();

        if (originDefault != null) {
            System.out.println("unset origin default role...");
            originDefault.setIsDefault(false);
            session.update(originDefault);
        }

        System.out.println("set new default role...");
        Role newDefault = getById(id);
        newDefault.setIsDefault(true);
        session.update(newDefault);
    }
}
